<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use App\Models\Company;

class CompanyRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $companyId = $this->route('company');
        if (!empty($companyId)) {
            return [
                'name' => 'required|unique:companies,name,'.$companyId.',_id|between:1,20',
                'email' =>'sometimes|regex:/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i',
                'alias' => 'required|unique:companies,alias,'.$companyId.',_id|between:1,20|regex:/^[a-zA-Z]+[a-zA-Z0-9_]*$/',
            ]; 
        } else {
            return [
                'name' => 'required|unique:companies|between:1,20',
                'alias' => 'required|unique:companies|between:1,20|regex:/^[a-zA-Z]+[a-zA-Z0-9_]*$/',
                'contact' => 'required',
                'email' =>'sometimes|regex:/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i',
                'contactTel' => 'required',
//                'startDate' => 'required',
//                'endDate' => 'required',
            ];
        }

        
    }

    public function messages()
    {
        return [
            'alias.regex' => '别名只能是英文和数字且不能以数字开头，限制20个字符',
            'alias.between' => '别名只能是英文和数字且不能以数字开头，限制20个字符',
            'email.regex' => '邮箱格式不正确',
        ];
    }
}
